package com.sunday.android.fangdaifu.bukong.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.Member;
import com.sunday.android.fangdaifu.bukong.entity.event.ResetPwd;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.BaseUtil;
import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class ForgetPwdActivity extends BaseActivity {
    @Bind(R.id.edt_phone)
    EditText mobile;
    @Bind(R.id.edt_code)
    EditText code;
    @Bind(R.id.btn_code)
    TextView btnCode;
    @Bind(R.id.btn_next)
    TextView btnNext;
    @Bind(R.id.title)
    TextView title;

    private TimeCount time;
    private String mobileText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivity_forget_pwd);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle!=null && !TextUtils.isEmpty(bundle.getString("title"))){
            title.setText(bundle.getString("title"));
        }
        time = new TimeCount(60000, 1000, btnCode);
    }

    @OnClick(R.id.btn_next)
    void nextBtn() {
        if (!StringUtils.isMobileNO(mobile.getText().toString())) {
            ToastUtils.showToast(mContext, "手机号码不合法");
            return;
        }
        mobileText = mobile.getText().toString();
        if (TextUtils.isEmpty(code.getText().toString())) {
            ToastUtils.showToast(mContext, "请输入验证码");
            return;
        }
        if(code.getText().toString().length() < 4){
            ToastUtils.showToast(mContext, "请输入验证码");
            return;
        }
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().AcValidateActiveCode(mobileText, code.getText().toString());
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    intent = new Intent(mContext,SettingPwdActivity.class);
                    intent.putExtra("userId",response.body().getResult().getId());
                    startActivity(intent);
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }


    @OnClick(R.id.btn_code)
    void getCode() {
        if (StringUtils.isMobileNO(mobile.getText().toString())) {
            mobileText = mobile.getText().toString();
            getcode();
        } else {
            ToastUtils.showToast(mContext, "手机号码不合法");
        }
    }

    private void getcode() {
        Call<ResultDO> call = ApiClient.getApiAdapter().AbSendActiveCode(mobileText);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    time.start();
                    ToastUtils.showToast(mContext, "发送成功，注意查收短信");
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });
    }

    public void onEvent(ResetPwd resetPwd){
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        time.onFinish();
    }

}

